package com.six666.medicaldevicetestingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six666.medicaldevicetestingsystem.dto.Equipment_Order_DTO;
import com.six666.medicaldevicetestingsystem.dto.Equipment_Scheme_DTO;
import com.six666.medicaldevicetestingsystem.dto.SampleList_number_DTO;
import com.six666.medicaldevicetestingsystem.dto.Sample_Order_DTO;
import com.six666.medicaldevicetestingsystem.entity.AccountOrder;
import com.six666.medicaldevicetestingsystem.entity.GoodsList;
import com.six666.medicaldevicetestingsystem.entity.SampleList;
import com.six666.medicaldevicetestingsystem.mapper.AccountOrder_Mapper;
import com.six666.medicaldevicetestingsystem.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class AccountOrder_Service_Impl extends ServiceImpl<AccountOrder_Mapper, AccountOrder> implements AccountOrder_Service {
    @Autowired
    GoodsList_Service goodslist_service;
    @Autowired
    Equipment_Scheme_Service equipment_scheme_service;
    @Autowired
    Account_Service account_service;
    @Autowired
    SampleList_Service samplelist_service;
    @Override
    public boolean add_order(int account_id, String order_type,int goods_id,int goods_amount) {
        try {
            if (order_type.equals("Sample") && samplelist_service.get_sample_by_id(goods_id) == null) {
                return false;
            }
            if (order_type.equals("Equipment") && equipment_scheme_service.get_by_id(goods_id) == null) {
                return false;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        QueryWrapper<AccountOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accountid",account_id);
        queryWrapper.eq("ordertype",order_type);
        queryWrapper.eq("orderstate","Preformed");
        AccountOrder ao=getOne(queryWrapper);
        try {
            if (ao == null) {
                ao = new AccountOrder();
                ao.setAccountid(account_id);
                ao.setOrdertype(order_type);
                ao.setOrderstate("Preformed");
                ao.setOrdertime(new Date(System.currentTimeMillis()));
                save(ao);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        ao=getOne(queryWrapper);
        return goodslist_service.add_good_list(ao.getOrderid(),goods_id,order_type,goods_amount);
    }

    @Override
    public List<Equipment_Order_DTO> get_personal_equip_order(int account_id,String state) {
        QueryWrapper<AccountOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("accountid",account_id);
        queryWrapper.eq("ordertype","Equipment");
        if (!Objects.equals(state, "OngoingAndFinish")) {
            queryWrapper.eq("orderstate", state);
            return getEquipmentOrderDtos(queryWrapper);
        }
        else {
            queryWrapper.eq("orderstate", "Finish");
            QueryWrapper<AccountOrder> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("accountid",account_id);
            queryWrapper1.eq("ordertype","Equipment");
            queryWrapper1.eq("orderstate", "Ongoing");
            List<Equipment_Order_DTO> dtos1 = getEquipmentOrderDtos(queryWrapper);
            List<Equipment_Order_DTO> dtos2 = getEquipmentOrderDtos(queryWrapper1);
            dtos1.addAll(dtos2);
            return dtos1;
        }
    }

    private List<Equipment_Order_DTO> getEquipmentOrderDtos(QueryWrapper<AccountOrder> queryWrapper) {
        List<AccountOrder> ao=list(queryWrapper);
        List<Equipment_Order_DTO> res=new ArrayList<>();
        for (AccountOrder ao2 : ao) {
            Equipment_Order_DTO temp=new Equipment_Order_DTO();
            temp.setOrder_id(ao2.getOrderid());
            temp.setUser(account_service.get_by_id(ao2.getAccountid()));
            temp.setOrder_state(ao2.getOrderstate());
            temp.setOrder_time(ao2.getOrdertime());
            List<GoodsList>goods=goodslist_service.get_goods_list(ao2.getOrderid());
            List<Equipment_Scheme_DTO> schems=new ArrayList<>();
            for (GoodsList goods2 : goods) {
                Equipment_Scheme_DTO temp2=equipment_scheme_service.get_equipment_scheme_by_id(goods2.getGoodsid());
                temp2.setNum(goods2.getGoodsamount());
                schems.add(temp2);
            }
            temp.setDetail(schems);
            res.add(temp);
        }
        return res;
    }

    @Override
    public boolean delete_order(int order_id) {
        QueryWrapper<AccountOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("orderid",order_id);
        boolean res=goodslist_service.delete_good_list(order_id);
        if (!res)return false;
        return remove(queryWrapper);
    }

    @Override
    public List<Equipment_Order_DTO> get_all_equip_order() {
        QueryWrapper<AccountOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordertype","Equipment");
        queryWrapper.eq("orderstate", "Finish");
        QueryWrapper<AccountOrder> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ordertype","Equipment");
        queryWrapper1.eq("orderstate", "Ongoing");
        List<Equipment_Order_DTO> dtos1 = getEquipmentOrderDtos(queryWrapper);
        List<Equipment_Order_DTO> dtos2 = getEquipmentOrderDtos(queryWrapper1);
        dtos1.addAll(dtos2);
        return dtos1;
    }

    @Override
    public boolean change_state(int order_id) {
        UpdateWrapper<AccountOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderid",order_id).set("orderstate","Finish");
        return update(updateWrapper);
    }
    @Override
    public boolean change_preformed_state(int order_id) {
        UpdateWrapper<AccountOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("orderid",order_id).set("orderstate","Ongoing");
        return update(updateWrapper);
    }
    private List<Sample_Order_DTO>get_sample_order_dtos(QueryWrapper<AccountOrder> queryWrapper) {
        List<AccountOrder> ao=list(queryWrapper);
        List<Sample_Order_DTO> res=new ArrayList<>();
        for (AccountOrder ao2 : ao) {
            Sample_Order_DTO temp=new Sample_Order_DTO();
            temp.setOrder_id(ao2.getOrderid());
            temp.setUser(account_service.get_by_id(ao2.getAccountid()));
            temp.setOrder_state(ao2.getOrderstate());
            temp.setOrder_time(ao2.getOrdertime());
            List<GoodsList>goods=goodslist_service.get_goods_list(ao2.getOrderid());
            List<SampleList_number_DTO> samples=new ArrayList<>();
            for (GoodsList goods2 : goods) {
                SampleList temp2=samplelist_service.get_sample_by_id(goods2.getGoodsid());

                SampleList_number_DTO temp3=new SampleList_number_DTO(temp2,goods2.getGoodsamount());

                samples.add(temp3);
            }
            temp.setDetail(samples);
            res.add(temp);
        }
        return res;
    }
    @Override
    public List<Sample_Order_DTO> get_all_sample_order() {
        QueryWrapper<AccountOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ordertype","Sample");
        queryWrapper.eq("orderstate", "Finish");
        QueryWrapper<AccountOrder> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("ordertype","Sample");
        queryWrapper1.eq("orderstate", "Ongoing");
        List<Sample_Order_DTO> dtos1 = get_sample_order_dtos(queryWrapper);
        List<Sample_Order_DTO> dtos2 = get_sample_order_dtos(queryWrapper1);
        dtos1.addAll(dtos2);
        return dtos1;
    }

    @Override
    public List<Sample_Order_DTO> get_my_order(Integer account_id,String type){
        System.out.println(type);
        String[] types=type.split("And");
        List<Sample_Order_DTO> res=new ArrayList<>();
        for (String s : types) {
            QueryWrapper<AccountOrder> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("accountid", account_id);
            queryWrapper.eq("ordertype", "Sample");
            queryWrapper.eq("orderstate", s);
            List<Sample_Order_DTO> dtos = get_sample_order_dtos(queryWrapper);
            res.addAll(dtos);
        }
        return res;
    }
}
